package org.example.dao;

import org.example.entity.Stuinfo;
import org.example.entity.Stuinfo;
import org.example.util.DButil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class StuinfoDao {
    public static ArrayList<Stuinfo> findAllStuinfo() {

        // 泛型
        ArrayList<Stuinfo> arrayList = new ArrayList<>();

        // 查询数据
        Connection connection = DButil.getConnection();
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        String sql = "select * from stuinfo ";

        try {
            // 获取预处理对象
            statement = connection.prepareStatement(sql);

            // 执行SQL语句
            resultSet = statement.executeQuery();

            while (resultSet.next()) {

                String Stuinfoid = resultSet.getString("Id");
                String Sname = resultSet.getString("Sname");
                String Sno = resultSet.getString("Sno");
                String Gender = resultSet.getString("Gender");
                String Major = resultSet.getString("Major");
                String Address = resultSet.getString("Address");
                String Grade = resultSet.getString("Grade");

                // 数据持久化 ==> 实体类
                Stuinfo stuinfo = new Stuinfo(Stuinfoid, Sname, Sno, Gender, Major, Address, Grade);

                arrayList.add(stuinfo);
            }

        } catch (java.sql.SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DButil.close(resultSet, statement, connection);
        }

        return arrayList;
    }

    public static int addStuinfo(Stuinfo stuinfo) {

        // 泛型

        // 查询数据
        Connection connection = DButil.getConnection();
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        String sql = "insert into stuinfo (Id,Sname,Sno,Gender,Major,Address,Grade) values (?,?,?,?,?,?,?)";
        int row = 0;
        try {
            // 获取预处理对象
            statement = connection.prepareStatement(sql);
            statement.setString(1, null);
            statement.setString(2, stuinfo.getSname());
            statement.setString(3, stuinfo.getSno());
            statement.setString(4, stuinfo.getGender());
            statement.setString(5, stuinfo.getMajor());
            statement.setString(6, stuinfo.getAddress());
            statement.setString(7, stuinfo.getGrade());


            // 执行SQL语句
            // select ----> statement.executeQuery();

            // insert delete update
            row = statement.executeUpdate();

        } catch (java.sql.SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DButil.close(resultSet, statement, connection);
        }

        return row;
    }

    public static int deleteStuinfo(String Id) {
        Connection connection = DButil.getConnection();
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        String sql = "delete from stuinfo where Id = ?";
        int row = 0;

        try {
            // 获取预处理对象
            statement = connection.prepareStatement(sql);
            statement.setString(1, Id);

            // 执行SQL语句
            // select ----> statement.executeQuery();

            // insert delete update
            row = statement.executeUpdate();

        } catch (java.sql.SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DButil.close(resultSet, statement, connection);
        }

        return row;
    }

    public static int updateStuinfo(Stuinfo stuinfo) {
        Connection connection = DButil.getConnection();
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        String sql = "update stuinfo set Sname=?,Sno = ?,Gender =  ?, Major = ?, Address = ?, Grade = ? where Id = ?";
        int row = 0;
        try {
            // 获取预处理对象
            statement = connection.prepareStatement(sql);
            statement.setString(1, stuinfo.getSname());
            statement.setString(2, stuinfo.getSno());
            statement.setString(3, stuinfo.getGender());
            statement.setString(4, stuinfo.getMajor());
            statement.setString(5, stuinfo.getAddress());
            statement.setString(6, stuinfo.getGrade());
            statement.setString(7, stuinfo.getId());


            // 执行SQL语句
            // select ----> statement.executeQuery();

            // insert delete update
            row = statement.executeUpdate();

        } catch (java.sql.SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DButil.close(resultSet, statement, connection);
        }

        return row;
    }

}
